package myProject.nim;

public class NimGame1 extends Rule{
    private int length;
    private int[] storeArray;
    private Human human;
    private Judge judge;
    private Machine1 machine;
    NimGame1(){
        judge = new Judge();
        this.length = judge.getTotalSets();
        storeArray = new int[length+1];
        human = new Human();
        machine = new Machine1();
    }
    public void start1(){
        System.out.println("-----------"+human.getName()+" VS "+machine.getName()+"-----------");
        judge.GeneratedRandomQueue(storeArray,length,1,100);
        judge.showStore("生成石子队列为：",storeArray,length);
        do {
            int idx1, idx2, num1, num2;
            //人类操作
            idx1 = human.getIdx(human.getName(), storeArray, length);
            num1 = human.getNumber(human.getName(), storeArray, idx1);
            if (isRemoveStore(storeArray, idx1, num1, length)) {
                length--;
            }
            if(judge.isEnd(length,human.getName(),machine.getName())){
                break;
            }
            judge.showStore("剩余石子：",storeArray,length);

            //机器操作
            int xorSum = machine.getXorSum(storeArray,length);
            if(xorSum == 0 ){
                idx2 = machine.getRandomInt(1,length + 1);
                num2 = machine.getRandomInt(1,storeArray[idx2-1]+1);
            }else {
                idx2 = machine.getOptimalIdx(storeArray, length, xorSum);
                num2 = machine.getOptimalNum(storeArray, idx2, xorSum);
            }
            judge.showOperation(machine.getName(),idx2,num2);
            if (isRemoveStore(storeArray, idx2, num2, length)) {
                length--;
            }
            if(judge.isEnd(length,machine.getName(),human.getName())){
                break;
            }
            judge.showStore("剩余石子：",storeArray,length);

        }while (length>0);
        System.out.println("结束");
    }

}
